package edu.ucsd.cse110;

import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;

/**
 * Displays the four types of WODs (Girl, Hero, Other, and Custom) as tabs on
 * the right screen of the main ViewPager.
 */
public class BenchmarkFragment extends Fragment {
	/**
	 * Points to the benchmark titles associated to each tab.
	 */
	private final int[] mTabTitleIds = { R.string.girl_title,
			R.string.hero_title, R.string.other_title, R.string.customwod_title };

	private LocalActivityManager mLam;
	private String mActivityId;

	/**
	 * TODO: Need documentation.
	 */
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		Bundle extras = data.getExtras();
		int position = extras.getInt("Position");
		int rating = extras.getInt("Rating");

		BenchmarkActivity activity = (BenchmarkActivity) mLam
				.getActivity(mActivityId);

		if (extras.getString("Name") != null) {
			activity.mListBenchmarks.get(position).setName(
					extras.get("Name").toString());
		}

		if (extras.getString("Description") != null) {
			activity.mListBenchmarks.get(position).setDescription(
					extras.getString("Description").toString());
		}

		activity.mListBenchmarks.get(position).setRating(rating);
		ListView list = activity.getListView();

		// Updates the name of the custom WOD.
		if (extras.getString("Name") != null) {
			TextView txtName = (TextView) list.getChildAt(
					position - list.getFirstVisiblePosition()).findViewById(
					R.id.benchmarkitem_txt_title);
			txtName.setText(extras.get("Name").toString());
		}

		RatingBar ratingBar = (RatingBar) list.getChildAt(
				position - list.getFirstVisiblePosition()).findViewById(
				R.id.benchmarkitem_rating);
		ratingBar.setRating(rating);
	}

	/**
	 * TODO: Need documentation.
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (container == null) {
			return null;
		}

		// Initializes local variables.
		View view = inflater.inflate(R.layout.benchmark, container, false);
		Activity activity = getActivity();
		Resources res = getResources();

		// Sets up LocalActivityManager for TabHost.
		mLam = new LocalActivityManager(getActivity(), false);
		mLam.dispatchCreate(savedInstanceState);
		mLam.dispatchResume();
		mLam.dispatchPause(activity.isFinishing());

		// Sets up TabHost.
		TabHost mTabHost = (TabHost) view.findViewById(R.id.benchmark_tabhost);
		mTabHost.setup(mLam);
		mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
			public void onTabChanged(String tabId) {
				mActivityId = tabId;
			}
		});

		// Adds tabs to TabHost.
		Intent intent;
		for (int i = 0; i < (mTabTitleIds.length); i++) {
			intent = new Intent().setClass(activity, BenchmarkActivity.class);
			intent.putExtra("Type", i);
			mTabHost.addTab(mTabHost.newTabSpec(res.getString(mTabTitleIds[i]))
					.setIndicator(res.getString(mTabTitleIds[i]))
					.setContent(intent));
		}

		/*
		 * // Adds a Custom WOD tab to TabHost. int type =
		 * Wod.Type.Custom.ordinal(); Intent intent = new
		 * Intent().setClass(activity, CustomBenchmarkActivity.class);
		 * intent.putExtra("Type", type);
		 * mTabHost.addTab(mTabHost.newTabSpec(res
		 * .getString(mTabTitleIds[type]))
		 * .setIndicator(res.getString(mTabTitleIds[type]))
		 * .setContent(intent));
		 */
		return view;
	}
}